//
//  LYTitleView.m
//  SelectCity
//
//  Created by liuyan on 2019/3/27.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYTitleView.h"

@interface LYTitleView ()

@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) UIButton *selectedBtn;
@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, strong) NSMutableArray *itemsArray;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UIColor *norColor;
@property (nonatomic, strong) UIColor *selColor;
@property (nonatomic, assign) CGFloat itemWidth;

@end

@implementation LYTitleView

- (NSMutableArray *)itemsArray {
    if (!_itemsArray) {
        _itemsArray = [NSMutableArray array];
    }
    return _itemsArray;
}
- (UIView *)indicatorView {
    if (!_indicatorView) {
        _indicatorView = [[UIView alloc] init];
        _indicatorView.backgroundColor = _selColor;
        _indicatorView.size = CGSizeMake(_itemWidth/2, 2);
    }
    return _indicatorView;
}
- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray norCorlor:(UIColor *)norCorlor selectColor:(UIColor *)selectColor {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = COLOR_OF_WHITE;
        _norColor = norCorlor;
        _selColor = selectColor;
        _titleArray = titlesArray;
        self.frame = frame;
        [self createSubview];
    }
    return self;
}

- (void)createSubview{
    _itemWidth = WIDTH*0.2;
    
    _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.height)];
    _contentView.backgroundColor = COLOR_OF_CLEAR;
    _contentView.scrollsToTop = NO;
    _contentView.bounces = NO;
    _contentView.showsHorizontalScrollIndicator = NO;
    _contentView.showsVerticalScrollIndicator = NO;
    [self addSubview:_contentView];
    [_contentView addSubview:self.indicatorView];
    _contentView.contentSize = CGSizeMake(_itemWidth * self.titleArray.count, self.height);
    
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 10000 + i;
        button.frame = CGRectMake(i * _itemWidth, 0, _itemWidth, self.height);
        [button setTitle: self.titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:_norColor forState:UIControlStateNormal];
        [button setTitleColor:_selColor forState:UIControlStateDisabled];
        button.titleLabel.font = Font(Font_14);
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:button];
        [self.itemsArray addObject:button];
        
        //默认选中第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedBtn = button;
        }
    }
    
    self.indicatorView.center = CGPointMake(self.selectedBtn.center.x, self.height - 1);
}

#pragma mark -- Click action
- (void)scrollToIndex:(NSInteger)index {
    NSInteger lastIndex = self.selectedBtn.tag;
    //修改按钮状态
    UIButton *button = self.itemsArray[index];
    //修改按钮状态
    self.selectedBtn.enabled = YES;
    button.enabled = NO;
    self.selectedBtn = button;
    
    if (lastIndex < self.selectedBtn.tag) {
        [self.contentView scrollRectToVisible:CGRectMake(button.left, button.top, 3.5*button.width, button.height) animated:YES];
    } else {
        [self.contentView scrollRectToVisible:CGRectMake(button.right-3.5*button.width, button.top, 3.5*button.width, button.height) animated:YES];
    }
    [self scrollAnimate];
}
- (void)titleClick:(UIButton *)sender {
    NSInteger lastIndex = self.selectedBtn.tag;
    
    //修改按钮状态
    self.selectedBtn.enabled = YES;
    sender.enabled = NO;
    self.selectedBtn = sender;
    
    if (_block) {
        _block(sender.tag - 10000);
    }
    
    if (lastIndex < self.selectedBtn.tag) {
        [self.contentView scrollRectToVisible:CGRectMake(sender.left, sender.top, 3.5*sender.width, sender.height) animated:YES];
    } else {
        [self.contentView scrollRectToVisible:CGRectMake(sender.right-3.5*sender.width, sender.top, 3.5*sender.width, sender.height) animated:YES];
    }
    
    [self scrollAnimate];
}

- (void)scrollAnimate {
    [UIView animateWithDuration:.1 animations:^{
        self.indicatorView.center = CGPointMake(self.selectedBtn.center.x, self.height - 1);
    }];
}

@end
